const path = require('path')
const webpack = require('webpack')
const {CleanWebpackPlugin} = require('clean-webpack-plugin')
const fs = require('fs')

const index = (function () {
    const jsx = fs.existsSync(path.join(__dirname, '../../../src/index.jsx'))
    const tsx = fs.existsSync(path.join(__dirname, '../../../src/index.tsx'))
    if (jsx) {
        return './src/index.jsx'
    }
    if (tsx) {
        return './src/index.tsx'
    }
    return './src/index.js'

})()

const argv = process.argv[2]

const base = {

    /**
     * webpack 4+ ，已经不需要vendor了。 使用optimization ！！！！！！
     */
    entry: {
        main: [index]
    },

    output: {
        path: path.join(__dirname, '../../../static'),

        filename: '[name].[chunkhash:5].js',

        publicPath: './',
    },

    module: {
        rules: [
            {
                test: /\.(ts|js)x?$/,
                exclude: /(node_modules|bower_components)/,
                use: [
                    {
                        loader: 'cache-loader',
                        options: {
                            cacheDirectory: '.cache'
                        }
                    },
                    {
                        loader: 'babel-loader',
                        options: {
                            presets: [
                                '@babel/preset-typescript',
                                ['@babel/preset-env', {"loose": true}],
                                '@babel/preset-react',
                            ],
                            plugins: [
                                ['@babel/plugin-proposal-decorators', {'legacy': true}],
                                ["@babel/plugin-proposal-private-methods", {"loose": true}],
                                ['@babel/plugin-proposal-class-properties', {'loose': true}],
                                ['@babel/plugin-proposal-private-property-in-object', {"loose": true}],
                                ['@babel/transform-runtime'],
                                ["import", {
                                    "libraryName": "antd",
                                    "libraryDirectory": "es",
                                    "style": true // `style: true` 会加载 less 文件
                                }]
                            ]
                        }
                    },
                ]
            },
            {
                test: /\.(jpg|png|gif|jpeg)$/,
                use:
                    {
                        loader: 'url-loader',
                        options: {
                            limit: 10000,
                            name: 'images/[hash:8].[name].[ext]'
                        }
                    }
            },
            {
                test: /\.(wav|mp3|mp4|ttf|otf|eot|woff2|woff)$/,
                use:
                    {
                        loader: 'file-loader'
                    }
            }
        ],

    },
    optimization: {
        runtimeChunk: {
            name: entrypoint => `rt~${entrypoint.name}`
        },
    },
    resolve: {

        modules: [
            'node_modules',
            'src'
        ],

        extensions: ['.js', '.jsx', '.ts', '.tsx'],

        alias: {},
    },

    context: path.join(__dirname, '../../../'),

    target: 'web',

    externals: {},

    stats: 'errors-only',

    plugins: [
        new CleanWebpackPlugin()
    ],
}

if (argv === 'build') {
    base.plugins.push(new webpack.ProgressPlugin())
}
module.exports = base;
